
local module = _G["BlinkHealthTextModule"]

if select(2, UnitClass("player")) ~= "MAGE" or not module then return end

local tcopy
local config_added = false

local L_FINGERS_OF_FROST = "서리의 손가락"
local L_PYROBLAST1 = "불덩이 작렬!"
local L_SHATTER = "산산조각"
local L_FROST_NOVA = "얼음 회오리"
local L_DEEP_FREEZE = "동결"
local L_ICE_LANCE = "얼음창"
local L_BRAIN_FREEZE = "두뇌 빙결"
local L_ARCANE_CHARGE = "비전 충전물"
local L_FROSTJAW = "서리투성이 턱"
local L_MAGE_CONFIG = "마법사 설정"
local L_USE_BRAIN_FREEZE = "|TInterface\\Icons\\Ability_Mage_Brainfreeze:20|t두뇌 빙결 발동시 |TInterface\\Icons\\Ability_Mage_Frostfirebolt:20|t얼음불꽃 화살 아이콘을 표시합니다."
local L_USE_SHATTER = "얼어붙은 효과시 대상 색상 |cffc0c0ff푸른색|r으로 변경"
local L_USE_SHATTER_DESC = "대상이 얼어붙었거나 |TInterface\\Icons\\Ability_Mage_Frostjaw:20|t서리투성이 턱, |TInterface\\Icons\\Ability_Mage_Wintersgrasp:20|t서리의 손가락, |TInterface\\Icons\\Spell_Frost_Frostshock:20|t산산조각 효과가 발동하면 대상의 체력을 |cffc0c0ff푸른색|r으로 보여줍니다."


local defaultDB = {
	db_ver = 1.0,
	use_shatter = true,
	use_swiftmend = true,
	use_savage_defense = true,
	use_brain_freeze = true,
}

module.config = {
	[L_FINGERS_OF_FROST] = {
		spellid = 112965
	},
	[L_PYROBLAST1] = {
		spellid = 11366
	},
	[L_BRAIN_FREEZE] = {
		spellid = 44549
	},
	[L_FROSTJAW] = {
		spellid = 102051,
	},
	[L_SHATTER] = {
		spellid = 12982
	},
	[L_DEEP_FREEZE] = {
		spellid = 44572
	},
}

-------------------------------------------------------------------------------
-- local functions
-------------------------------------------------------------------------------

function tcopy(to, from) -- "to" must be a table (possibly empty)
	for k,v in pairs(from) do
		if(type(v)=="table") then
			if not to then to = {} end
			to[k] = {}
			tcopy(to[k], v)
		else
			to[k] = v
		end
	end
end

local function getSize(baseSize, expirationTime, duration)
	local size = (expirationTime-GetTime()) / duration
	if size < 0.3 then
		size = baseSize * .8
	elseif size < 0.6 then
		size = baseSize * 1.0
	else
		size = baseSize * 1.2
	end
	return size / 2
end


-------------------------------------------------------------------------------
-- module functions
-------------------------------------------------------------------------------
function module:init()
	if self.addon.db then
		if not self.addon.db.class or not self.addon.db.class.db_ver or self.addon.db.class.db_ver < defaultDB.db_ver then
			self.addon.db.class = {}
			tcopy(self.addon.db.class, defaultDB)
		end
	end

	self.addon.mainFrame:RegisterEvent("PLAYER_ALIVE")
	self.addon.mainFrame:RegisterEvent("LEARNED_SPELL_IN_TAB")
	self.addon.mainFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
	self:update()

	if not config_added then
		self:AddMiscConfig{
			type = "description",
			text = L_MAGE_CONFIG,
			fontobject = "QuestTitleFont",
			r = 1,
			g = 0.82,
			b = 0,
			justifyH = "LEFT",
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_SHATTER,
			tooltip = L_USE_SHATTER_DESC,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_shatter
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_shatter = value
			end,
			needRefresh = true,
		}
		self:AddMiscConfig{
			type = "toggle",
			text = L_USE_BRAIN_FREEZE,
			tooltip = L_USE_BRAIN_FREEZE,
			get = function ()
				if self.addon and self.addon.db then
					return self.addon.db.class.use_brain_freeze
				end
				return
			end,
			set = function (value)
				self.addon.db.class.use_brain_freeze = value
			end,
			needRefresh = true,
		}
		
--		self:AddMiscConfig{
--			type = "toggle",
--			text = L_USE_SAVAGE_DEFENSE,
--			--tooltip = L_USE_COMBO_DESC,
--			get = function ()
--				if self.addon and self.addon.db then
--					return self.addon.db.class.use_savage_defense
--				end
--				return
--			end,
--			set = function (value)
--				self.addon.db.class.use_savage_defense = value
--			end,
--			needRefresh = true,
--		}
--		self:AddMiscConfig{
--			type = "toggle",
--			text = L_USE_SWIFTMEND,
--			--tooltip = L_USE_SWIFTMEND_DESC,
--			get = function ()
--				if self.addon and self.addon.db then
--					return self.addon.db.class.use_swiftmend
--				end
--			end,
--			set = function (value)
--				self.addon.db.class.use_swiftmend = value
--			end,
--			needRefresh = true,
--		}
		config_added = true
	end
end

function module:update()
	self.primary = GetSpecialization()
	for auraName, t in pairs(self.config) do
		if t.talent and t.talent > 0 then
			local spellid, name, iconTexture, selected, available = GetTalentInfo(t.talent);
			if name and selected then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		else
			self.config[auraName].use = false
			if IsPlayerSpell(t.spellid) then
				self.config[auraName].use = true
				if t.event and self.addon and self.addon.mainFrame then
					self.addon.mainFrame:RegisterEvent(t.event)
				end
			end
		end
	end
end

function module:LEARNED_SPELL_IN_TAB(...)
	self:update()
end
module.CHARACTER_POINTS_CHANGED = module.LEARNED_SPELL_IN_TAB
function module:PLAYER_ALIVE(...)
	self.addon.mainFrame:UnregisterEvent("PLAYER_ALIVE")
	self:update()
end

function module:getTargetText()
	local text = ""
	local size
	local name, rank, icon, count, debufType, duration, expirationTime
	local r, g, b = 1, 1, 1

	if self.config[L_PYROBLAST1].use then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_PYROBLAST1, nil, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|T%s:%d|t"):format(icon, size)
		end
	end

	if self.config[L_BRAIN_FREEZE].use and self.addon.db.class.use_brain_freeze then
		name, rank, icon, count, debufType, duration, expirationTime = UnitAura("player", L_BRAIN_FREEZE, nil, "HELPFUL")
		if name then
			size = getSize(self.addon.db.fontSizeHealth, expirationTime, duration)
			text = text .. (":|TInterface\\Icons\\Ability_Mage_Frostfirebolt:%d|t"):format(size)
		end
	end

--	if self.config[L_DEEP_FREEZE].use then
--		name = nil
--		if self.config[L_FINGERS_OF_FROST].use then
--			name = UnitAura("player", L_FINGERS_OF_FROST, nil, "HELPFUL")
--		end
--		if IsUsableSpell(L_DEEP_FREEZE) and (name or UnitAura("target", L_FROST_NOVA, nil, "HARMFUL")) then
--			text = text .. (":|TInterface\\Icons\\Ability_Mage_DeepFreeze:%d|t"):format( self.addon.db.fontSizeHealth / 2)
--		end
--	end

	return text
end

function module:getTargetHealthColor()
	local name

	if self.config[L_SHATTER].use and self.addon.db.class.use_shatter then
		if self.config[L_FINGERS_OF_FROST].use then
			name = UnitAura("player", L_FINGERS_OF_FROST, nil, "HELPFUL")
			if name then
				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
			end
		end
--		if self.config[L_BRAIN_FREEZE].use then
--			name = UnitAura("player", L_BRAIN_FREEZE, nil, "HELPFUL")
--			if name then
--				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
--			end
--		end
		name = UnitAura("target", L_FROST_NOVA, nil, "HARMFUL")
		if name then
			return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
		end
		if self.config[L_FROSTJAW].use then
			name = UnitAura("target", L_FROSTJAW, nil, "HARMFUL")
			if name then
				return true, 0.75, 0.75, 1.0 -- 파란색? 하늘색?
			end
		end
	end
end

function module:getPlayerText()
	if self.primary ~= 1 then
		return ""
	end
	return self:getAuraCount("player", "비전 충전물", "HARMFUL")
end

